package com.tvsinfo.jdk.demo.case9_fature;

import java.util.concurrent.*;

/**
 * 老版本处理
 *
 * @Author ruyi.song
 * @Date 2019/12/30 17:11:57
 */
public class Future1_V5_Test {

    public static void main(String[] args) {
        Future1_V5_Test future1_v5_test=new Future1_V5_Test();
        try {
            future1_v5_test.test();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void test() throws ExecutionException, InterruptedException {
        //创建线程池
        ExecutorService executorService = Executors.newFixedThreadPool(10);
        //事情1
        Callable<String> callable = () -> {
            Thread.sleep(1900L);
            return new String("samething 1 done");
        };
        Future<String> future = executorService.submit(callable);

        //事情2
        Future<String> future2 = executorService.submit(() ->{
            Thread.sleep(1800L);
            return new String("samething 2 done");
        } );
        //事情3
        Future<String> future3 = executorService.submit(() ->{
            Thread.sleep(1060L);
            System.out.println(1/0);
            return new String("samething 3 done");
        } );

//        //轮询结果1
        for (;;) {
            if (future.isDone()){
                System.out.println("结果："+future.get()); break;
            }
        }
        //轮询结果2
        for (;;) {
            if (future2.isDone()){
                System.out.println("结果："+future2.get());  break;
            }
        }

        //轮询结果3
        for (;;) {
            System.out.println("---------------isCancelled:"+future3.isCancelled()+"  isDone:"+future3.isDone());
            if (future3.isDone()){
                System.out.println("---------------");
                System.out.println("结果："+future3.get());break;
            }
        }
        executorService.shutdown();
    }

}
